package com.example.guiguiapplicatio.fragment.FamilySigning.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.FamilyarChooseService;
import com.example.guiguiapplicatio.Response.ServicePackageResponse;

import java.util.List;

public class FamilySigningServiceAdapter extends RecyclerView.Adapter<FamilySigningServiceAdapter.FamilySigningServiceViewHolder>{
//    private List<ServicePackageResponse> packageResponseList;
    private OnServicePackageSelectedListener listener;
    private FamilyarChooseService familyarchooseService;

    public FamilySigningServiceAdapter(FamilyarChooseService familyarchooseService, OnServicePackageSelectedListener listener) {
        this.familyarchooseService = familyarchooseService;
        this.listener = listener;
    }

    @NonNull
    @Override
    public FamilySigningServiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_total_detail_family_sign_item_serveitem, parent, false);
        return new FamilySigningServiceAdapter.FamilySigningServiceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FamilySigningServiceViewHolder holder, int position) {
        ServicePackageResponse servicePackageResponse = familyarchooseService.getServicePackageResponseList().get(position);
        holder.examination_name.setText(servicePackageResponse.getName());
        holder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                familyarchooseService.getServicePackageResponseList().remove(servicePackageResponse);
                // Notify adapter of the item removal
                notifyDataSetChanged();
                // Notify the listener of the updated list
                listener.onServicePackagesSelected(familyarchooseService);
            }
        });
    }

    @Override
    public int getItemCount() {
         return familyarchooseService.getServicePackageResponseList().size();
    }



    public class FamilySigningServiceViewHolder extends RecyclerView.ViewHolder {
        TextView examination_name;
        ImageView remove;
        LinearLayout linearLayout;

        public FamilySigningServiceViewHolder(@NonNull View itemView) {
            super(itemView);
            examination_name = itemView.findViewById(R.id.examination_name);
            remove = itemView.findViewById(R.id.remove);
            linearLayout = itemView.findViewById(R.id.linearLayout);
        }

        public int getItemWidth() {
            if (linearLayout != null) {
                return linearLayout.getWidth();
            } else {
                return 0;
            }
        }
    }
}
